package com.liushuai.phoneguardian;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.liushuai.phoneguardian.service.ShowAddressService;
import com.liushuai.phoneguardian.ui.SettingView;
import com.liushuai.phoneguardian.utils.ActivityUtils;
import com.liushuai.phoneguardian.utils.ServiceStatusUtils;

public class SettingActivity extends Activity implements OnClickListener {

	private SettingView st_setting_view;
	private SettingView st_setting_showaddress;
	private SharedPreferences sp;
	private Intent showAddressInent;
	private RelativeLayout rl_setting_changebg;
	private TextView tv_setting_addressbg_color;
	private static String[] items = { "半透明", "活力橙", "卫士蓝", "金属灰", "苹果绿" };
	private RelativeLayout rl_setting_change_location;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		boolean isupdate = sp.getBoolean("update", true);

		st_setting_view = (SettingView) findViewById(R.id.st_setting_view);
		st_setting_view.setChecked(isupdate);
		st_setting_view.setOnClickListener(new OnClickListener() {

			Editor editor = sp.edit();

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (st_setting_view.isChecked()) {
					st_setting_view.setChecked(false);
					editor.putBoolean("update", false);
				} else {
					st_setting_view.setChecked(true);
					editor.putBoolean("update", true);
				}
				editor.commit();
			}
		});

		// 号码归属地设置
		st_setting_showaddress = (SettingView) findViewById(R.id.st_setting_showaddress);
		showAddressInent = new Intent(this, ShowAddressService.class);

		st_setting_showaddress.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (st_setting_showaddress.isChecked()) {
					st_setting_showaddress.setChecked(false);
					stopService(showAddressInent);
				} else {
					st_setting_showaddress.setChecked(true);
					startService(showAddressInent);
				}

			}
		});
		// 更改背景颜色的控件初始化
		rl_setting_changebg = (RelativeLayout) findViewById(R.id.rl_setting_changebg);
		rl_setting_changebg.setOnClickListener(this);
		tv_setting_addressbg_color = (TextView) findViewById(R.id.tv_setting_addressbg_color);

		int which = sp.getInt("which", 0);
		tv_setting_addressbg_color.setText(items[which]);

		// 更改归属地位置
		rl_setting_change_location = (RelativeLayout) findViewById(R.id.rl_setting_change_location);
		rl_setting_change_location.setOnClickListener(this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		// 重新初始化服务的状态
		if (ServiceStatusUtils.isServiceRunning(
				"com.liushuai.phoneguardian.service.ShowAddressService", this)) {
			st_setting_showaddress.setChecked(true);
		} else {
			st_setting_showaddress.setChecked(false);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_setting_changebg:
			showChangeBgDialog();
			break;
		case R.id.rl_setting_change_location:
			ActivityUtils.startActivity(this, DragViewActivity.class);
			break;
		}

	}

	private void showChangeBgDialog() {
		// TODO Auto-generated method stub
		AlertDialog.Builder builder = new Builder(this);
		builder.setIcon(R.drawable.notification);
		builder.setTitle("归属地提示框风格");
		int which = sp.getInt("which", 0);
		builder.setSingleChoiceItems(items, which,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Editor editor = sp.edit();
						editor.putInt("which", which);
						editor.commit();
						dialog.dismiss();
						tv_setting_addressbg_color.setText(items[which]);
					}
				});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.show();
	}

}
